//
//  BCPutCoinNumbersView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/29.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCPutCoinNumbersView.h"

#import "DDCustomNumberInputView.h"

@interface BCPutCoinNumbersView ()

@property (nonatomic, strong) DDCustomNumberInputView *customInputView;

@end

@implementation BCPutCoinNumbersView

- (DDCustomNumberInputView *)customInputView {
    if (!_customInputView) {
        _customInputView = [[DDCustomNumberInputView alloc] initWithFrame:[DDCustomNumberInputView defaultBounds]];
    }
    return _customInputView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.customInputView.textField = self.textField;
        //
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.textField];
    }
    return self;
}

#pragma mark - textFieldTextDidChangeNotification:
- (void)textFieldTextDidChangeNotification:(NSNotification *)noti {
    id object = noti.object;
    if (object) {
        if (object==self.textField) {
            NSString *text = self.textField.text;
            if (text.length==1&&[text isEqualToString:@"."]) {//第一个输入.时 补0
                self.textField.text = @"0.";
            }
            if (text.length>1) {
                //只能输入到小数点后两位
                NSRange pointRange = [text rangeOfString:@"."];
                if (pointRange.location!=NSNotFound) {//有小数点 就不能输入小数点了
                    //判断小数点是不是最后一位
                    NSString *lastChar = [text substringFromIndex:text.length-1];
                    if (pointRange.location==text.length-1&&[lastChar isEqualToString:@"."]) {//说明输入的最后一位是小数点
                        
                    }else if([lastChar isEqualToString:@"."]) {
                        //如果最后一位是小数点 并且已经有了小数点 不能输入
                        text = [text substringToIndex:text.length-1];
                        self.textField.text = text;
                    }
                    //小数点后几位
                    NSInteger pointNumber = self.digits;
                    if (text.length-(pointRange.location+1)>pointNumber) {
                        self.textField.text = [text substringToIndex:pointRange.location+pointNumber+1];
                    }
                }else {//没有小数点 位数不能超过100000
//                    if (text.length>6) {
//                        self.textField.text = [text substringToIndex:6];
//                    }
                }
            }
            //
            if (self.delegate && [self.delegate respondsToSelector:@selector(putCoinNumbersViewTextDidChanged:)]) {
                [self.delegate putCoinNumbersViewTextDidChanged:self.textField.text];
            }
            
        }
    }
}




- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.textField];
}

@end
